<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;


/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class RenwuController extends AdminController {
    //目标统计
    public function index(){
		 if(I('time_start') && I('time_end')){
            $begin       = I('time_start');
            $end         = I('time_end');
            $xbegin      = date("m",strtotime($begin));
            $xend        = date("m",strtotime($end));
            $x = 0;
			for ($i=$xbegin; $i <= $xend ; $i++) { 
				$shijian[] = date("Y-m",strtotime("$begin +$x month"));
				$x++;
			}
            $this->assign('begin',$begin);
            $this->assign('end',$end);
        }else{
        	$attime = date("Y-m",time());
          	$x = 0;
			while($x<5) {
			  $shijian[] = date("Y-m",strtotime("$attime -$x month"));
			  $x++;
			}  
        }
       	$renwu = D('Renwu');

		 
		
        foreach ($shijian as $key => $value) {
        	$sj = $this->gaintime(substr($value,0,4),substr($value,5,6));
        	$data[$key]['time']  = $value;
        	$data[$key]['hscj']  = $this->map($sj['firstday'],$sj['lastday'],'cj','hunsha');
        	$data[$key]['yscj']  = $this->map($sj['firstday'],$sj['lastday'],'cj','yishu');
			$data[$key]['gpcj']  = $this->map($sj['firstday'],$sj['lastday'],'cj','genpai');
        	$data[$key]['zj']    = $data[$key]['yscj']+$data[$key]['hscj']+$data[$key]['gpcj'];
        	$data[$key]['jd']    = $this->map($sj['firstday'],$sj['lastday'],'jd');
        	$data[$key]['cj']    = $this->map($sj['firstday'],$sj['lastday'],'cj');
        	$mubiao          = $renwu->where(array('time'=>strtotime($value)))->find();
        	$data[$key]['huoqu'] = $mubiao['huoqu'];
        	$data[$key]['hunsha']= $mubiao['hunsha'];
			$data[$key]['genpai']= $mubiao['genpai'];
        	$data[$key]['zongji']= $mubiao['zongji'];
        	$data[$key]['yishu'] = $mubiao['zongji']-$mubiao['hunsha']-$mubiao['genpai'];
        }
        foreach ($data as $key => $value) {
        	$zj['hscj']   += $value['hscj'];
        	$zj['yscj']   += $value['yscj'];
			$zj['gpcj']   += $value['gpcj'];
			$zj['zj']     += $value['zj'];
            $zj['cj']     += $value['cj'];
            $zj['jd']     += $value['jd'];
            $zj['huoqu']  += $value['huoqu'];
        	$zj['hunsha'] += $value['hunsha'];
			$zj['genpai'] += $value['genpai'];
			$zj['zongji'] += $value['zongji'];
            $zj['yishu']  += $value['yishu'];
        }
     $this->assign('zj',$zj);   
	 $this->assign('data',$data);
	 $this->display();
	}

	//时间转换方法
	public function gaintime($y='',$m=''){
		 if($y=="") $y=date("Y");
		 if($m=="") $m=date("m");
		 $m=sprintf("%02d",intval($m));
		 $y=str_pad(intval($y),4,"0",STR_PAD_RIGHT);
		 
		 $m>12||$m<1?$m=1:$m=$m;
		 $firstday=strtotime($y.$m."01000000");
		 $firstdaystr=date("Y-m-01",$firstday);
		 $lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
		 return array("firstday"=>$firstday,"lastday"=>$lastday);
	}
    //查询总计
    public function map($begin,$end,$mm,$lb=''){
            $map['puth']         =  array('eq','1');
            if($lb == 'hunsha'){
            	 $map['lb']   = array('eq','31');
            }
            if($lb == 'yishu'){
            	 //不包含爱情微电影和跟拍
            	 $map['lb']   = array('in','124,125,126,123');
            }
			if($lb == 'genpai'){
            	 //不包含爱情微电影和跟拍
            	 $map['lb']   = array('in','113,114');
            }

            if($mm == 'jd'){
            	$map['ywstate']   = array('not in','66');
				$map['kf']   = array('gt','1');
                $map['jdtime']   = array('between',array($begin,$end));
            }


            if($mm == 'cj'){
                $map['ywstate']       = array('in','115,151,67');
                $map['ddtime']   = array('between',array($begin,$end));
            }
            $data   = D('Yewu')->where($map)->count();
            return $data;

    }

}
